package com.rp.selfUser.test.controller;


import com.rp.common.result.ResultDTO;
import com.rp.common.utils.CreateRandom;
import com.rp.selfUser.test.service.OneselfService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;

/**
 * 类说明:
 *
 * @author ****
 * @date 2020-11-23 11:47  新建
 */
@RestController
@RequestMapping("/test")
@Api(tags = "自用测试接口")

@RefreshScope

public class OnselfController {

    @Autowired
    private OneselfService oneselfService;

    @Value("${datasource.driverClassName}")
    private String driverClassName;
    @Value("${datasource.url}")
    private String dbUrl;
    @Value("${datasource.username}")
    private String username;
    @Value("${datasource.password}")
    private String password;
    @Value("${applicationName}")
    private String applicationName;

    @ApiOperation(value = "mybatisPlus自动构建文件")
    @GetMapping("/create")
    public ResultDTO create(@RequestParam @ApiParam(value = "表名", required = true, example = "sys_user") String tableName,
                            @RequestParam @ApiParam(value = "包路径", required = true, example = "com.rp.it") String url,
                            @RequestParam(required = false) @ApiParam(value = "模块名") String modelName) {
        return ResultDTO.success().message(oneselfService.create(tableName, url,modelName));
    }


    @ApiOperation(value = "获取数据库表字段")
    @GetMapping("/getFields")
    public String getFields(@RequestParam @ApiParam(value = "表名", required = true, example = "sys_user") String tableName) {
        Connection conn = null;
        try {
            Class.forName(driverClassName);
            conn = DriverManager.getConnection(dbUrl, username, password);
            String sql = "select * from " + tableName;
            PreparedStatement stmt;
            stmt = conn.prepareStatement(sql);
            StringBuffer stringBuffer = new StringBuffer();
            ResultSet rs2 = stmt.executeQuery("show full columns from " + tableName);
            while (rs2.next()) {
                stringBuffer.append(rs2.getString("Field") + ",");
            }
            return stringBuffer.toString().substring(0, stringBuffer.toString().length()-1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    @ApiOperation(value = "获取32位UUID")
    @GetMapping("/createRandom")
    public String createRandom() {
        return CreateRandom.createRandomCharData(32);
    }


    @ApiOperation(value = "获取ncaos中的配置文件")
    @GetMapping("/getNacos")
    public String getNacos() {
        return applicationName;
    }


}
